<!DOCTYPE html>
<html>
<head><#include "/common/common.ftl" parse=true encoding="utf-8">
</style>
</head>
<body>
	
	<#include "/common/header.ftl" parse=true encoding="utf-8">
	<div class="container">
		<div class="row"  style="margin-top: 5px;">
			<form id="myForm" method="post" class="form-horizontal">
				<p style="text-align: center;"><font style="font-size: large;">成才网登录</font></p>
				<div class="form-group">
					<label for="email" class="col-md-4 control-label">邮箱</label>
					<div class="col-md-4">
						<input type="email" class="form-control" id="email" name="email"
							placeholder="请输入您的电子邮箱地址">
					</div>
				</div>
				<div class="form-group">
					<label for="password" class="col-md-4 control-label">密码</label>
					<div class="col-md-4">
						<input type="password" class="form-control" id="password"
							name="password" placeholder="Password">
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-4 control-label">验证码</label>
					<div class="col-md-4">
						<input type="text" class="form-control" name="randomCode"
							id="randomCode" placeholder="验证码" onblur="valify()" />
					</div>
					<div>
						<span class="error"></span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-4 control-label"></label>
					<div class="col-md-4">
						<input type="button" name="code" id="code" onclick="createCode()" />
						<a href="javascript:createCode();">看不清?点击图片换一换</a>
					</div>
				</div>
				<div class="form-group">
					<div class="col-md-offset-4 col-md-8">
						<button type="submit" class="btn btn-default">登录</button>
					</div>
				</div>
			</form>
		</div>
	</div>

	<#include "/common/footer.ftl" parse=true encoding="utf-8">
</body>

<script src="${base}/scripts/js/md5.js"></script>
<script type="text/javascript">
	/* 验证码模块 */
	//设置一个全局的变量，便于保存验证码
	var code;
	function createCode() {
		//首先默认code为空字符串
		code = '';
		//设置长度，这里看需求，我这里设置了4
		var codeLength = 4;
		var codeV = document.getElementById('code');
		//设置随机字符
		var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C',
				'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
				'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
		//循环codeLength 我设置的4就是循环4次
		for (var i = 0; i < codeLength; i++) {
			//设置随机数范围,这设置为0 ~ 36
			var index = Math.floor(Math.random() * 36);
			//字符串拼接 将每次随机的字符 进行拼接
			code += random[index];
		}
		//将拼接好的字符串赋值给展示的Value
		codeV.value = code;
	}

	$(function() {
		$('#myForm').bootstrapValidator(
				{
					message : '该项不能为空',
					live : 'disabled',
					feedbackIcons : {
						validating : 'glyphicon glyphicon-refresh'
					},
					submitHandler : function(validator, form, submitButton) {
						if (valify() == true) {
							var url = "${base}/auth/loginAction.json";
							var entity = {
								email : $("#email").val(),
								password : md5($("#password").val())
							};
							var onloadFunc = function(result) {
								if (result.isSuccess == true) {
									window.location = "${base}/index.html";
								} else {
									window.alert(result.message);
									createCode();
								}
							};
							httpRequestPostSubmitJson(form, url, entity, false,
									onloadFunc);
						}
					} ,	fields : {
							email : {
								message : '邮箱格式错误',
								validators : {
									notEmpty : {
										message : '邮箱地址不能为空'
									},
									emailAddress: {
				                        message: '邮箱格式不正确'
				                    }
								}
							},
							password : {
								validators : {
									notEmpty : {
										message : '密码不能为空'
									}
								}
							}
					}
				});
	});

	/***********验证码*********/
	function valify() {
		var oValue = $('#randomCode').val().trim().toUpperCase();
		if ($('#randomCode').val().trim() === '') {
			$('span.error').show();
			$('span.error').html('验证码不能为空！');
			createCode();
			return false;
		}
		if (oValue == code) {
			$('span.error').hide();
			return true;
		} else {
			$('span.error').show();
			$('span.error').html('验证码不正确！');
			createCode();
			return false;
		}
	}

	//设置此处的原因是每次进入界面展示一个随机的验证码，不设置则为空
	window.onload = function() {
		createCode();
	}
</script>

</html>